#region " using "
using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Threading;
using System.Globalization;
using System.Text;
using System.IO;
using Smarty;
using System.Web;
#endregion

public partial class Ccategory_Add : AspNetRunnerPage 
{
    string filename = string.Empty;
    string status = string.Empty;
    string msg = string.Empty;
    string linkdata = string.Empty;
    string formname = string.Empty;
    string onsubmit = string.Empty;
    string bodyonload = string.Empty;
    bool error_happened=false;
    IList<string> showKeys = new List<string>();
    IList<string> showKeyValues = new List<string>();
    IList<string> showValues = new List<string>();
    IList<string> showRawValues = new List<string>();
    IList<string> showFields = new List<string>();
    IDictionary<string, string> showDetailKeys = new Dictionary<string, string>();
    IDictionary<string, object> rdonlyfields = new Dictionary<string, object>();
    IDictionary<string, object> body = new Dictionary<string, object>();
    IDictionary<string, object> defvalues = new Dictionary<string, object>();
    IList<string> arr_includes = new List<string>();
    ADD_MODE inlineedit;
    string templatefile;
    bool needvalidate;
    object record_id;
    bool isCopy = false;
    
    Data.categoryController controller = new Data.categoryController();
    Data.category item = null;

    protected void Page_Init( object sender,  System.EventArgs e)  
    {
        strTableName = "category";
        strTableNameLocale = "category";
    }

    protected void Page_Load( object sender,  System.EventArgs e)  
    {
                CheckSecurity();
        Init();
        if(RequestAction == "added")
        {
            try
            {
                SaveData();
            }
            catch(Exception saveEx)
            {
                msg = saveEx.Message;
                error_happened = true;
                ShowFailMessage();
            }
        }
        CopyRecord();
        BuildBody();
        Message();
        ReadonlyFields();
        Wizards();
        BuildBodyEnd();
        BuildForm();
        Message();

        if(RequestAction == "added")
        {
            if(inlineedit != ADD_MODE.ADD_INLINE && inlineedit != ADD_MODE.ADD_ONTHEFLY)
            {
                output.Append(func.BuildOutput(this, @"~\admin\" + templatefile, smarty));
            }
        }
        else
        {
            output.Append(func.BuildOutput(this, @"~\admin\" + templatefile, smarty));
        }
        this.Response.Write(output.ToString());
        this.Response.End();
    }

    private void ShowSuccessMessage()
    {
        if ( inlineedit == ADD_MODE.ADD_INLINE ) 
		{
			status ="ADDED";
			msg = "Record was added";
		} 
		else
        {
			msg = "<div class=message><<< " + "Record was added" + " >>></div>";
        }
    }

    private void ShowFailMessage()
    {
        if ( inlineedit == ADD_MODE.ADD_INLINE ) 
		{
			msg = "Record was NOT added" + ": " + msg;
		} 
		else
        {
			msg = "<div class=message><<< " + "Record was NOT added" + ": " + msg + " >>></div>";
        }
    }

    private void SaveData()
    {
        item = new Data.category();
                //	processing CateID - start
        //                if(!string.IsNullOrEmpty(Request["value_CateID"]))
        {
                                                                                                item.CateID = /*Convert.ToString(Request["value_CateID"])*/ Guid.NewGuid().ToString();
                    }
                //	processing CateName - start
                        if(!string.IsNullOrEmpty(Request["value_CateName"]))
        {
                                                                                                item.CateName = Convert.ToString(Request["value_CateName"]);
                    }
                //	processing CateParent - start
                        if(!string.IsNullOrEmpty(Request["value_CateParent"]))
        {
                                                                                                item.CateParent = Convert.ToString(Request["value_CateParent"]);
                    }
                //	processing Description - start
                        if(!string.IsNullOrEmpty(Request["value_Description"]))
        {
                                                                                                item.Description = Convert.ToString(Request["value_Description"]);
                    }
                //	processing VipLevel - start
                        if(!string.IsNullOrEmpty(Request["value_VipLevel"]))
        {
                                                                                                item.VipLevel = Convert.ToInt32(Request["value_VipLevel"]);
                    }
                //	processing Active - start
                        if(!string.IsNullOrEmpty(Request["value_Active"]))
        {
                                                                                                item.Active = bool.Parse(Convert.ToString(Request["value_Active"]));
                    }
                //	processing Price - start
                        if(!string.IsNullOrEmpty(Request["value_Price"]))
        {
                                                                                                item.Price = Convert.ToDouble(Request["value_Price"]);
                    }
                //	processing Image - start
                        if(!string.IsNullOrEmpty(Request["value_Image"]))
        {
                                                                                                item.Image = Convert.ToString(Request["value_Image"]);
                    }
                //	processing Status - start
                        if(!string.IsNullOrEmpty(Request["value_Status"]))
        {
                                                                                                item.Status = Convert.ToInt32(Request["value_Status"]);
                    }
                //	processing CreatedDate - start
        //                if(!string.IsNullOrEmpty(Request["value_CreatedDate"]))
        {
                                                                                                item.CreatedDate = /*Convert.ToDateTime(Request["value_CreatedDate"])*/ DateTime.Now;
                    }
                //	processing UpdatedDate - start
        //                if(!string.IsNullOrEmpty(Request["value_UpdatedDate"]))
        {
                                                                                                item.UpdatedDate = /*Convert.ToDateTime(Request["value_UpdatedDate"])*/ DateTime.Now;
                    }
                bool abortSaving = false;
        
        
        if(!abortSaving)
        {
            item.Save();
                        ShowSuccessMessage();
        }
    }

    private void BuildForm()
    {
        /////////////////////////////////////////////////////////////
        //	prepare Edit Controls
        /////////////////////////////////////////////////////////////
        if(RequestAction == "added")
        {
            string masterquery="";
        	
	        showKeys.Add(Control.HTMLEncodeSpecialChars(item.CateID.ToString()));

	        string keylink="";
	        keylink +="&key1=" + Control.HTMLEncodeSpecialChars(this.Server.UrlEncode(item.CateID.ToString()));

            string value="";

            value="";
            Control control_CateID = new Control("CateID", item.CateID, false, smarty, this.Request, builder, MODE.MODE_LIST);
	        ////////////////////////////////////////////
	        //	CateID - 
		        
		                                value = control_CateID.GetData();
			            value = control_CateID.ProcessLargeText(value,"field=CateID" + keylink,"",MODE.MODE_LIST);
		        showValues.Add(value);
		        showFields.Add("CateID");
		        		        showRawValues.Add(string.Empty);
            value="";
            Control control_CateName = new Control("CateName", item.CateName, false, smarty, this.Request, builder, MODE.MODE_LIST);
	        ////////////////////////////////////////////
	        //	CateName - 
		        
		                                value = control_CateName.GetData();
			            value = control_CateName.ProcessLargeText(value,"field=CateName" + keylink,"",MODE.MODE_LIST);
		        showValues.Add(value);
		        showFields.Add("CateName");
		        		        showRawValues.Add(string.Empty);
            value="";
            Control control_CateParent = new Control("CateParent", item.CateParent, false, smarty, this.Request, builder, MODE.MODE_LIST);
	        ////////////////////////////////////////////
	        //	CateParent - 
		        
		                                func.PopulateLookupFields(control_CateParent.FieldInfo);
			            value=control_CateParent.DisplayLookupWizard();
		        showValues.Add(value);
		        showFields.Add("CateParent");
		        		        showRawValues.Add(string.Empty);
            value="";
            Control control_Description = new Control("Description", item.Description, false, smarty, this.Request, builder, MODE.MODE_LIST);
	        ////////////////////////////////////////////
	        //	Description - 
		        
		                                value = control_Description.GetData();
			            value = control_Description.ProcessLargeText(value,"field=Description" + keylink,"",MODE.MODE_LIST);
		        showValues.Add(value);
		        showFields.Add("Description");
		        		        showRawValues.Add(string.Empty);
            value="";
            Control control_VipLevel = new Control("VipLevel", item.VipLevel, false, smarty, this.Request, builder, MODE.MODE_LIST);
	        ////////////////////////////////////////////
	        //	VipLevel - 
		        
		                                value = control_VipLevel.GetData();
			            value = control_VipLevel.ProcessLargeText(value,"field=VipLevel" + keylink,"",MODE.MODE_LIST);
		        showValues.Add(value);
		        showFields.Add("VipLevel");
		        		        showRawValues.Add(string.Empty);
            value="";
            Control control_Active = new Control("Active", item.Active, false, smarty, this.Request, builder, MODE.MODE_LIST);
	        ////////////////////////////////////////////
	        //	Active - Checkbox
		        
		        			            value = control_Active.GetData();
		        showValues.Add(value);
		        showFields.Add("Active");
		        		        showRawValues.Add(string.Empty);
            value="";
            Control control_Price = new Control("Price", item.Price, false, smarty, this.Request, builder, MODE.MODE_LIST);
	        ////////////////////////////////////////////
	        //	Price - Number
		        
		                                value = control_Price.GetData();
			            value = control_Price.ProcessLargeText(value,"field=Price" + keylink,"",MODE.MODE_LIST);
		        showValues.Add(value);
		        showFields.Add("Price");
		        		        showRawValues.Add(string.Empty);
            value="";
            Control control_Image = new Control("Image", item.Image, false, smarty, this.Request, builder, MODE.MODE_LIST);
	        ////////////////////////////////////////////
	        //	Image - 
		        
		                                value = control_Image.GetData();
			            value = control_Image.ProcessLargeText(value,"field=Image" + keylink,"",MODE.MODE_LIST);
		        showValues.Add(value);
		        showFields.Add("Image");
		        		        showRawValues.Add(string.Empty);
            value="";
            Control control_Status = new Control("Status", item.Status, false, smarty, this.Request, builder, MODE.MODE_LIST);
	        ////////////////////////////////////////////
	        //	Status - 
		        
		                                value = control_Status.GetData();
			            value = control_Status.ProcessLargeText(value,"field=Status" + keylink,"",MODE.MODE_LIST);
		        showValues.Add(value);
		        showFields.Add("Status");
		        		        showRawValues.Add(string.Empty);
            value="";
            Control control_CreatedDate = new Control("CreatedDate", item.CreatedDate, false, smarty, this.Request, builder, MODE.MODE_LIST);
	        ////////////////////////////////////////////
	        //	CreatedDate - Short Date
		        
		                                value = control_CreatedDate.GetData();
			            value = control_CreatedDate.ProcessLargeText(value,"field=CreatedDate" + keylink,"",MODE.MODE_LIST);
		        showValues.Add(value);
		        showFields.Add("CreatedDate");
		        		        showRawValues.Add(string.Empty);
            value="";
            Control control_UpdatedDate = new Control("UpdatedDate", item.UpdatedDate, false, smarty, this.Request, builder, MODE.MODE_LIST);
	        ////////////////////////////////////////////
	        //	UpdatedDate - Short Date
		        
		                                value = control_UpdatedDate.GetData();
			            value = control_UpdatedDate.ProcessLargeText(value,"field=UpdatedDate" + keylink,"",MODE.MODE_LIST);
		        showValues.Add(value);
		        showFields.Add("UpdatedDate");
		        		        showRawValues.Add(string.Empty);
        }

        if(RequestAction =="added" && inlineedit == ADD_MODE.ADD_ONTHEFLY)
        {
            output.Append("<textarea id=\"data\">");
		    output.Append("added");
            output.Append(Control.print_inline_array(showKeys));
            output.Append("\n");
		    output.Append(Control.print_inline_array(showKeyValues));
            output.Append("\n");
	        output.Append("</textarea>");
        }

        if ( RequestAction =="added" && inlineedit == ADD_MODE.ADD_INLINE ) 
        {
	        output.Append("<textarea id=\"data\">");
	        if(showValues.Count > 0  && !error_happened)
	        {
			    output.Append("saved");
		        output.Append(Control.print_inline_array(showKeys));
                output.Append("\n");
		        output.Append(Control.print_inline_array(showValues));
                output.Append("\n");
		        output.Append(Control.print_inline_array(showFields));
                output.Append("\n");
		        output.Append(Control.print_inline_array(showRawValues));
                output.Append("\n");
		        output.Append(Control.print_inline_array(showDetailKeys.Values));
                output.Append("\n");
	        }
	        else
	        {
		        if(status=="DECLINED")
                {
			        output.Append("decli");
                }
		        else
                {
			        output.Append("error");
                    output.Append(msg);
                }
	        }
	        output.Append("</textarea>");
        }
        else
        {
            MODE ctrlMode;
            IDictionary<string, object> detailkeys = new Dictionary<string, object>();
            
            object CateID_value = null;
            if(defvalues.ContainsKey("CateID"))
            {
                CateID_value = defvalues["CateID"];
            }
            else if(isCopy)
            {
                CateID_value = item.CateID;
            }
            if(inlineedit == ADD_MODE.ADD_INLINE)
            {
                ctrlMode = MODE.MODE_INLINE_ADD;
            }
            else
            {
                ctrlMode = MODE.MODE_ADD;
            }
            Control edit_control_CateID = null;

            if(!string.IsNullOrEmpty(Mastertable) && (detailkeys.ContainsKey("CateID")))
            {
                edit_control_CateID = new Control("CateID", detailkeys["CateID"], false, smarty, this.Request, builder, ctrlMode);
            }
            else
            {
                                edit_control_CateID = new Control("CateID", CateID_value, false, smarty, this.Request, builder, ctrlMode);
            }

                                    smarty.Add("CateID_editcontrol", edit_control_CateID.BuildEditControl());
            object CateName_value = null;
            if(defvalues.ContainsKey("CateName"))
            {
                CateName_value = defvalues["CateName"];
            }
            else if(isCopy)
            {
                CateName_value = item.CateName;
            }
            if(inlineedit == ADD_MODE.ADD_INLINE)
            {
                ctrlMode = MODE.MODE_INLINE_ADD;
            }
            else
            {
                ctrlMode = MODE.MODE_ADD;
            }
            Control edit_control_CateName = null;

            if(!string.IsNullOrEmpty(Mastertable) && (detailkeys.ContainsKey("CateName")))
            {
                edit_control_CateName = new Control("CateName", detailkeys["CateName"], false, smarty, this.Request, builder, ctrlMode);
            }
            else
            {
                                edit_control_CateName = new Control("CateName", CateName_value, false, smarty, this.Request, builder, ctrlMode);
            }

                                    smarty.Add("CateName_editcontrol", edit_control_CateName.BuildEditControl());
            object CateParent_value = null;
            if(defvalues.ContainsKey("CateParent"))
            {
                CateParent_value = defvalues["CateParent"];
            }
            else if(isCopy)
            {
                CateParent_value = item.CateParent;
            }
            if(inlineedit == ADD_MODE.ADD_INLINE)
            {
                ctrlMode = MODE.MODE_INLINE_ADD;
            }
            else
            {
                ctrlMode = MODE.MODE_ADD;
            }
            Control edit_control_CateParent = null;

            if(!string.IsNullOrEmpty(Mastertable) && (detailkeys.ContainsKey("CateParent")))
            {
                edit_control_CateParent = new Control("CateParent", detailkeys["CateParent"], false, smarty, this.Request, builder, ctrlMode);
            }
            else
            {
                                edit_control_CateParent = new Control("CateParent", CateParent_value, false, smarty, this.Request, builder, ctrlMode);
            }

                        func.PopulateLookupFields(edit_control_CateParent.FieldInfo);
                        smarty.Add("CateParent_editcontrol", edit_control_CateParent.BuildEditControl());
            object Description_value = null;
            if(defvalues.ContainsKey("Description"))
            {
                Description_value = defvalues["Description"];
            }
            else if(isCopy)
            {
                Description_value = item.Description;
            }
            if(inlineedit == ADD_MODE.ADD_INLINE)
            {
                ctrlMode = MODE.MODE_INLINE_ADD;
            }
            else
            {
                ctrlMode = MODE.MODE_ADD;
            }
            Control edit_control_Description = null;

            if(!string.IsNullOrEmpty(Mastertable) && (detailkeys.ContainsKey("Description")))
            {
                edit_control_Description = new Control("Description", detailkeys["Description"], false, smarty, this.Request, builder, ctrlMode);
            }
            else
            {
                                edit_control_Description = new Control("Description", Description_value, false, smarty, this.Request, builder, ctrlMode);
            }

                                    smarty.Add("Description_editcontrol", edit_control_Description.BuildEditControl());
            object VipLevel_value = null;
            if(defvalues.ContainsKey("VipLevel"))
            {
                VipLevel_value = defvalues["VipLevel"];
            }
            else if(isCopy)
            {
                VipLevel_value = item.VipLevel;
            }
            if(inlineedit == ADD_MODE.ADD_INLINE)
            {
                ctrlMode = MODE.MODE_INLINE_ADD;
            }
            else
            {
                ctrlMode = MODE.MODE_ADD;
            }
            Control edit_control_VipLevel = null;

            if(!string.IsNullOrEmpty(Mastertable) && (detailkeys.ContainsKey("VipLevel")))
            {
                edit_control_VipLevel = new Control("VipLevel", detailkeys["VipLevel"], false, smarty, this.Request, builder, ctrlMode);
            }
            else
            {
                                edit_control_VipLevel = new Control("VipLevel", VipLevel_value, false, smarty, this.Request, builder, ctrlMode);
            }

                                    smarty.Add("VipLevel_editcontrol", edit_control_VipLevel.BuildEditControl());
            object Active_value = null;
            if(defvalues.ContainsKey("Active"))
            {
                Active_value = defvalues["Active"];
            }
            else if(isCopy)
            {
                Active_value = item.Active;
            }
            if(inlineedit == ADD_MODE.ADD_INLINE)
            {
                ctrlMode = MODE.MODE_INLINE_ADD;
            }
            else
            {
                ctrlMode = MODE.MODE_ADD;
            }
            Control edit_control_Active = null;

            if(!string.IsNullOrEmpty(Mastertable) && (detailkeys.ContainsKey("Active")))
            {
                edit_control_Active = new Control("Active", detailkeys["Active"], false, smarty, this.Request, builder, ctrlMode);
            }
            else
            {
                                edit_control_Active = new Control("Active", Active_value, false, smarty, this.Request, builder, ctrlMode);
            }

                                    smarty.Add("Active_editcontrol", edit_control_Active.BuildEditControl());
            object Price_value = null;
            if(defvalues.ContainsKey("Price"))
            {
                Price_value = defvalues["Price"];
            }
            else if(isCopy)
            {
                Price_value = item.Price;
            }
            if(inlineedit == ADD_MODE.ADD_INLINE)
            {
                ctrlMode = MODE.MODE_INLINE_ADD;
            }
            else
            {
                ctrlMode = MODE.MODE_ADD;
            }
            Control edit_control_Price = null;

            if(!string.IsNullOrEmpty(Mastertable) && (detailkeys.ContainsKey("Price")))
            {
                edit_control_Price = new Control("Price", detailkeys["Price"], false, smarty, this.Request, builder, ctrlMode);
            }
            else
            {
                                edit_control_Price = new Control("Price", Price_value, false, smarty, this.Request, builder, ctrlMode);
            }

                                    smarty.Add("Price_editcontrol", edit_control_Price.BuildEditControl());
            object Image_value = null;
            if(defvalues.ContainsKey("Image"))
            {
                Image_value = defvalues["Image"];
            }
            else if(isCopy)
            {
                Image_value = item.Image;
            }
            if(inlineedit == ADD_MODE.ADD_INLINE)
            {
                ctrlMode = MODE.MODE_INLINE_ADD;
            }
            else
            {
                ctrlMode = MODE.MODE_ADD;
            }
            Control edit_control_Image = null;

            if(!string.IsNullOrEmpty(Mastertable) && (detailkeys.ContainsKey("Image")))
            {
                edit_control_Image = new Control("Image", detailkeys["Image"], false, smarty, this.Request, builder, ctrlMode);
            }
            else
            {
                                edit_control_Image = new Control("Image", Image_value, false, smarty, this.Request, builder, ctrlMode);
            }

                                    smarty.Add("Image_editcontrol", edit_control_Image.BuildEditControl());
            object Status_value = null;
            if(defvalues.ContainsKey("Status"))
            {
                Status_value = defvalues["Status"];
            }
            else if(isCopy)
            {
                Status_value = item.Status;
            }
            if(inlineedit == ADD_MODE.ADD_INLINE)
            {
                ctrlMode = MODE.MODE_INLINE_ADD;
            }
            else
            {
                ctrlMode = MODE.MODE_ADD;
            }
            Control edit_control_Status = null;

            if(!string.IsNullOrEmpty(Mastertable) && (detailkeys.ContainsKey("Status")))
            {
                edit_control_Status = new Control("Status", detailkeys["Status"], false, smarty, this.Request, builder, ctrlMode);
            }
            else
            {
                                edit_control_Status = new Control("Status", Status_value, false, smarty, this.Request, builder, ctrlMode);
            }

                                    smarty.Add("Status_editcontrol", edit_control_Status.BuildEditControl());
            object CreatedDate_value = null;
            if(defvalues.ContainsKey("CreatedDate"))
            {
                CreatedDate_value = defvalues["CreatedDate"];
            }
            else if(isCopy)
            {
                CreatedDate_value = item.CreatedDate;
            }
            if(inlineedit == ADD_MODE.ADD_INLINE)
            {
                ctrlMode = MODE.MODE_INLINE_ADD;
            }
            else
            {
                ctrlMode = MODE.MODE_ADD;
            }
            Control edit_control_CreatedDate = null;

            if(!string.IsNullOrEmpty(Mastertable) && (detailkeys.ContainsKey("CreatedDate")))
            {
                edit_control_CreatedDate = new Control("CreatedDate", detailkeys["CreatedDate"], false, smarty, this.Request, builder, ctrlMode);
            }
            else
            {
                                edit_control_CreatedDate = new Control("CreatedDate", CreatedDate_value, false, smarty, this.Request, builder, ctrlMode);
            }

                                    smarty.Add("CreatedDate_editcontrol", edit_control_CreatedDate.BuildEditControl());
            object UpdatedDate_value = null;
            if(defvalues.ContainsKey("UpdatedDate"))
            {
                UpdatedDate_value = defvalues["UpdatedDate"];
            }
            else if(isCopy)
            {
                UpdatedDate_value = item.UpdatedDate;
            }
            if(inlineedit == ADD_MODE.ADD_INLINE)
            {
                ctrlMode = MODE.MODE_INLINE_ADD;
            }
            else
            {
                ctrlMode = MODE.MODE_ADD;
            }
            Control edit_control_UpdatedDate = null;

            if(!string.IsNullOrEmpty(Mastertable) && (detailkeys.ContainsKey("UpdatedDate")))
            {
                edit_control_UpdatedDate = new Control("UpdatedDate", detailkeys["UpdatedDate"], false, smarty, this.Request, builder, ctrlMode);
            }
            else
            {
                                edit_control_UpdatedDate = new Control("UpdatedDate", UpdatedDate_value, false, smarty, this.Request, builder, ctrlMode);
            }

                                    smarty.Add("UpdatedDate_editcontrol", edit_control_UpdatedDate.BuildEditControl());
        }
        smarty.Add("fullurl", Page.Request.Url.AbsoluteUri.Substring(0, Page.Request.Url.AbsoluteUri.LastIndexOf('/') + 1));
    }

    private void BuildBodyEnd()
    {
        if(inlineedit != ADD_MODE.ADD_ONTHEFLY)
        {
	        body["end"] = "</form>" + linkdata + "<script>" + bodyonload + "</script>";
	        smarty.Add("body", body);
	        smarty.Add("flybody",true);
        }
        else
        {
	        smarty.Add("flybody", body);
	        smarty.Add("body", true);
        }
    }

    private void Wizards()
    {
        record_id= Request["recordID"];
        object value = null;
        if(defvalues.ContainsKey(""))
        {
            value = defvalues[""];
        }
        object fvalue = null;
        if(defvalues.ContainsKey("ArtID"))
        {
            fvalue = defvalues["ArtID"];
        }
        if (useAJAX)
        {
            if(inlineedit == ADD_MODE.ADD_ONTHEFLY)
            {
                record_id = Request["id"];
            }
            Smarty.Table tableInfo = null;
            Smarty.Field fieldInfo = null;
            string txt = "";
            List<Smarty.LookupField> lookups = null;

            if ( inlineedit == ADD_MODE.ADD_INLINE ) 
	        {
		        linkdata = linkdata.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");
            }
	        else 
	        {
		        linkdata += "SetToFirstControl('" + formname + "');";
		        if(inlineedit == ADD_MODE.ADD_SIMPLE)
		        {
			        linkdata = "<script type=\"text/javascript\">\r\n" +
			        "$(document).ready(function(){ \r\n" +
			        linkdata +
			        "});</script>";
		        }
		        else
		        {
			        linkdata = GetIncludes()  + "\r\n" + linkdata;
			        string includes="var s;";
			        foreach(string file in arr_includes)
			        {
			            includes += "s = document.createElement('script');s.src = '" + file + @"';\r\n" +
			            "document.getElementsByTagName('HEAD')[0].appendChild(s);\r\n";
			        }			
			        linkdata = includes + "\r\n" + linkdata;

			        if(RequestAction != "added")
			        {
                        linkdata = linkdata.Replace("\\", "\\\\").Replace("\r", "\\r").Replace("\n", "\\n");
			            Response.Write(linkdata);
			            Response.Write("\n");
			        }
			        else if(RequestAction == "added" && (error_happened || status=="DECLINED"))
			        {
			            Response.Write("<textarea id=\"data\">decli");
			            Response.Write(Control.HTMLEncodeSpecialChars(linkdata));
			            Response.Write("</textarea>");
			        }

		        }
	        }
        }
        else 
        {
        }
    }

    private void CopyRecord()
    {
        //	copy record
        if(Request["copyid1"] != null || Request["editid1"] != null)
        {
	        if(Request["copyid1"] != null)
	        {
                item = Data.category.FetchByID(Request["copyid1"]);
                isCopy = true;
	        }
	        else
	        {
                 item = Data.category.FetchByID(Request["editid1"]);
	        }

                        //	clear key fields
	            defvalues["CateID"] = item.CateID;
        }
        else if(defvalues.Count == 0)
        {
	        defvalues["Active"] = "True";
	        defvalues["Status"] = 0;
            defvalues["VipLevel"] = 0;
        }

        if(inlineedit == ADD_MODE.ADD_ONTHEFLY)
        {
        }
    }

    private void ReadonlyFields()
    {
        //	show readonly fields
    }

    private void Message()
    {
        if(!string.IsNullOrEmpty(msg))
        {
	        smarty["message_block"] = true;
	        smarty["message"] = msg;
        }
    }

    private void BuildBody()
    {
	    formname = "editform";
	    if(inlineedit != ADD_MODE.ADD_ONTHEFLY)
	    {
            string includes = GetIncludes();
		    if(!string.IsNullOrEmpty(onsubmit))
            {
			    onsubmit = "onsubmit=\"" + onsubmit + "\"";
            }
		    body["begin"] = includes + 
		    "<form name=\"editform\" encType=\"multipart/form-data\" method=\"post\" action=\"category_add.aspx\" " + onsubmit + ">" +
		    "<input type=hidden name=\"a\" value=\"added\">";
		    smarty.Add("backbutton_attrs","onclick=\"window.location.href='category_list.aspx?a=return'\"");
		    smarty.Add("back_button",true);
	    }
	    else
	    {
		    formname = "editform" + (string)Request["id"];
		    body["begin"]= "<form name=\"editform" + (string)Request["id"] + "\" encType=\"multipart/form-data\" method=\"post\" action=\"category_add.aspx\" " + onsubmit + " target=\"flyframe" + (string)Request["id"] + "\">" +
		    "<input type=hidden name=\"a\" value=\"added\">" + 
		    "<input type=hidden name=\"editType\" value=\"onthefly\">" + 
		    "<input type=hidden name=\"table\" value=\"" + (string)Request["table"] + "\">" +
		    "<input type=hidden name=\"field\" value=\"" + (string)Request["field"] + "\">" +
		    "<input type=hidden name=\"category\" value=\"" + (string)Request["category"] + "\">" +
            "<input type=hidden name=\"id\" value=\"" + (string)Request["id"] + "\">";
		    smarty.Add("cancelbutton_attrs","onclick=\"RemoveFlyDiv('" + (string)Request["id"] + "');\"");
		    smarty.Add("cancel_button",true);
	    }
	    smarty.Add("save_button",true);
	    smarty.Add("reset_button",true);
    }

    private string GetIncludes()
    {
        StringBuilder includes = new StringBuilder();
        if ( inlineedit != ADD_MODE.ADD_INLINE ) 
        {
            string validatetype = string.Empty;
            needvalidate = false;

            if(inlineedit != ADD_MODE.ADD_ONTHEFLY)
            {
		        includes.Append("<script language=\"JavaScript\" src=\"include/validate.js\"></script>\r\n");
                includes.Append("<script type=\"text/javascript\">\r\n");
	            includes.AppendFormat("var TEXT_FIELDS_REQUIRED='{0}';\r\n", Control.jsreplace("The Following fields are Required"));
	            includes.AppendFormat("var TEXT_FIELDS_ZIPCODES='{0}';\r\n", Control.jsreplace("The Following fields must be valid Zipcodes"));
	            includes.AppendFormat("var TEXT_FIELDS_EMAILS='{0}';\r\n", Control.jsreplace("The Following fields must be valid Emails"));
	            includes.AppendFormat("var TEXT_FIELDS_NUMBERS='{0}';\r\n", Control.jsreplace("The Following fields must be Numbers"));
	            includes.AppendFormat("var TEXT_FIELDS_CURRENCY='{0}';\r\n", Control.jsreplace("The Following fields must be currency"));
	            includes.AppendFormat("var TEXT_FIELDS_PHONE='{0}';\r\n", Control.jsreplace("The Following fields must be Phone Numbers"));
	            includes.AppendFormat("var TEXT_FIELDS_PASSWORD1='{0}';\r\n",Control.jsreplace("The Following fields must be valid Passwords"));
	            includes.AppendFormat("var TEXT_FIELDS_PASSWORD2='{0}';\r\n", Control.jsreplace("should be at least 4 characters long"));
	            includes.AppendFormat("var TEXT_FIELDS_PASSWORD3='{0}';\r\n", Control.jsreplace("Cannot be 'password'"));
	            includes.AppendFormat("var TEXT_FIELDS_STATE='{0}';\r\n", Control.jsreplace("The Following fields must be State Names"));
	            includes.AppendFormat("var TEXT_FIELDS_SSN='{0}';\r\n", Control.jsreplace("The Following fields must be Social Security Numbers"));
	            includes.AppendFormat("var TEXT_FIELDS_DATE='{0}';\r\n", Control.jsreplace("The Following fields must be valid dates"));
	            includes.AppendFormat("var TEXT_FIELDS_TIME='{0}';\r\n", Control.jsreplace("The Following fields must be valid time in 24-hours format"));
	            includes.AppendFormat("var TEXT_FIELDS_CC='{0}';\r\n", Control.jsreplace("The Following fields must be valid Credit Card Numbers"));
	            includes.Append( "</script>\r\n");
            }
            else
            {
                includes.Append("<script type=\"text/javascript\">\r\n");
	            includes.AppendFormat("var TEXT_INLINE_FIELD_REQUIRED='{0}';\r\n", Control.jsreplace("Required field"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_ZIPCODE='{0}';\r\n", Control.jsreplace("Field should be a valid zipcode"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_EMAIL='{0}';\r\n", Control.jsreplace("Field should be a valid email address"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_NUMBER='{0}';\r\n", Control.jsreplace("Field should be a valid number"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_CURRENCY='{0}';\r\n", Control.jsreplace("Field should be a valid currency"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_PHONE='{0}';\r\n", Control.jsreplace("Field should be a valid phone number"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_PASSWORD1='{0}';\r\n",Control.jsreplace("Field can not be 'password'"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_PASSWORD2='{0}';\r\n", Control.jsreplace("Field should be at least 4 characters long"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_STATE='{0}';\r\n", Control.jsreplace("Field should be a valid US state name"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_SSN='{0}';\r\n", Control.jsreplace("Field should be a valid Social Security Number"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_DATE='{0}';\r\n", Control.jsreplace("Field should be a valid date"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_TIME='{0}';\r\n", Control.jsreplace("Field should be a valid time in 24-hour format"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_CC='{0}';\r\n", Control.jsreplace("Field should be a valid credit card number"));
	            includes.AppendFormat("var TEXT_INLINE_FIELD_SSN='{0}';\r\n", Control.jsreplace("Field should be a valid Social Security Number"));
	            includes.Append( "</script>\r\n");
            }

            
            			        validatetype = string.Empty;
		        		        if(!string.IsNullOrEmpty(validatetype))
		        {
			        needvalidate = true;
			        if(inlineedit == ADD_MODE.ADD_ONTHEFLY)
                    {
				        linkdata += "define_fly('value_VipLevel_" + (string)Request["id"] + "','" + validatetype + "');";
                    }
			        else
                    {
				        bodyonload += "define('value_VipLevel','" + validatetype + "','Vip Level');";
                    }
		        }

            
            			        			            validatetype="IsNumeric";
		        		        if(!string.IsNullOrEmpty(validatetype))
		        {
			        needvalidate = true;
			        if(inlineedit == ADD_MODE.ADD_ONTHEFLY)
                    {
				        linkdata += "define_fly('value_Price_" + (string)Request["id"] + "','" + validatetype + "');";
                    }
			        else
                    {
				        bodyonload += "define('value_Price','" + validatetype + "','Price');";
                    }
		        }

            
            			        validatetype = string.Empty;
		        		        if(!string.IsNullOrEmpty(validatetype))
		        {
			        needvalidate = true;
			        if(inlineedit == ADD_MODE.ADD_ONTHEFLY)
                    {
				        linkdata += "define_fly('value_Status_" + (string)Request["id"] + "','" + validatetype + "');";
                    }
			        else
                    {
				        bodyonload += "define('value_Status','" + validatetype + "','Status');";
                    }
		        }

            if(needvalidate)
	        {
		        if(inlineedit == ADD_MODE.ADD_ONTHEFLY)
                {
			        onsubmit = "return validate_fly(this);";
                }
		        else
                {
			        onsubmit = "return validate();";
                }
		        //bodyonload = "onload=\"" + bodyonload + "\"";
	        }

            if(inlineedit != ADD_MODE.ADD_ONTHEFLY)
	        {
		        includes.Append("<script language=\"JavaScript\" src=\"include/jquery.js\"></script>\r\n");
		        includes.Append("<script language=\"JavaScript\" src=\"include/onthefly.js\"></script>\r\n");
		        if (useAJAX) 
			        includes.Append("<script language=\"JavaScript\" src=\"include/ajaxsuggest.js\"></script>\r\n");
		        includes.Append("<script language=\"JavaScript\" src=\"include/jsfunctions.js\"></script>\r\n");
	        }
	        if(inlineedit != ADD_MODE.ADD_ONTHEFLY)
	        {
		        includes.Append("<script language=\"JavaScript\">\r\n");
	        }
	        includes.Append("var locale_dateformat = '" + Control.locale_info("LOCALE_IDATE", smarty) + "';\r\n" +
	        "var locale_datedelimiter = \"" + Control.locale_info("LOCALE_SDATE", smarty) + "\";\r\n" + 
	        "var bLoading=false;\r\n" + 
	        "var TEXT_PLEASE_SELECT='" + Control.AddSlashes("Please select") + "';\r\n");
	        if (useAJAX) 
            {
	            includes.Append("var SUGGEST_TABLE='category_searchsuggest.aspx';\r\n");
	        }
	        if(inlineedit != ADD_MODE.ADD_ONTHEFLY)
	        {
		        includes.Append("</script>\r\n");
		        if (useAJAX)
			        includes.Append("<div id=\"search_suggest\"></div>\r\n");
	        }

	        	        //	include datepicker files
	        if(inlineedit != ADD_MODE.ADD_ONTHEFLY)
            {
		        includes.Append("<script language=\"JavaScript\" src=\"include/calendar.js\"></script>\r\n");
            }
	        else
            {
		        arr_includes.Add("include/calendar.js");
            }
	        
            


            smarty.Add("CateID_fieldblock",true);
            smarty.Add("CateName_fieldblock",true);
            smarty.Add("CateParent_fieldblock",true);
            smarty.Add("Description_fieldblock",true);
            smarty.Add("VipLevel_fieldblock",true);
            smarty.Add("Active_fieldblock",true);
            smarty.Add("Price_fieldblock",true);
            smarty.Add("Image_fieldblock",true);
            smarty.Add("Status_fieldblock",true);
            smarty.Add("CreatedDate_fieldblock",true);
            smarty.Add("UpdatedDate_fieldblock",true);
        }

        return includes.ToString();
    }

    private void Init()
    {
        if((string)Request["editType"] == "inline")
        {
	        inlineedit = ADD_MODE.ADD_INLINE;
        }
        else if((string)Request["editType"] == "onthefly")
        {
	        inlineedit = ADD_MODE.ADD_ONTHEFLY;
        }
        else
        {
	        inlineedit = ADD_MODE.ADD_SIMPLE;
        }
        if(inlineedit == ADD_MODE.ADD_INLINE)
        {
	        templatefile = "category_inline_add.aspx";
        }
        else
        {
	        templatefile = "category_add.aspx";
        }
    }

    private string Mastertable
    {
        get
        {
            return (string)SessionPropertyGet(strTableName + "_mastertable", string.Empty);
        }
        set
        {
            SessionPropertySet(strTableName + "_mastertable", value);
        }
    }

        private bool CheckSecurity()
    {
        //	check if logged in
                if(string.IsNullOrEmpty(UserName) && func.IsAdminUser() && !(func.CheckSecurity(strTableName, "Add", OwnerID)))
        {
            this.Response.Write("<p>" + "You don't have permissions to access this table" + "<br>Proceed to <a href=\"admin.aspx'\">Admin Area</a> to set up user permissions</p>");
            this.Response.End();
            return false;
        }

        if(string.IsNullOrEmpty(UserName) || !func.CheckSecurity(strTableName, "Add", OwnerID))
        { 
            MyUrl = this.Request.AppRelativeCurrentExecutionFilePath;
            this.Server.Transfer("~/admin/login.aspx?message=expired");
            return false;
        }
        return true;
    }
}

